package com.tacitknowledge.filters.clustercheck;

import com.tacitknowledge.filters.clustercheck.ClusterCheckingHttpSessionWrapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

class ClusterCheckingHttpServletRequest extends HttpServletRequestWrapper {
	private ClusterCheckingHttpSessionWrapper wrapper = null;

	public ClusterCheckingHttpServletRequest(HttpServletRequest request) {
		super(request);
	}

	public HttpSession getSession() {
		return this.getSession(true);
	}

	public HttpSession getSession(boolean arg0) {
		HttpSession session = super.getSession(arg0);
		if (session != null) {
			if (this.wrapper == null || session.getId() != this.wrapper.getWrappedSession().getId()) {
				this.wrapper = new ClusterCheckingHttpSessionWrapper(session);
			}

			session = this.wrapper.getWrappedSession();
		}

		return session;
	}

	public List<String> validateSession() {
		return (List<String>) (this.wrapper != null ? this.wrapper.validate() : new LinkedList<String>());
	}

	public Map<String, Integer> getAttributeSizes() throws IOException {
		return (Map<String, Integer>) (this.wrapper != null ? this.wrapper.getAttributeSizes() : new HashMap<String, Integer>());
	}

	public int getAggregateSize() throws IOException {
		return this.wrapper != null ? this.wrapper.getAggregateSize() : -1;
	}
}